package com.holmeslei.greendaodemo.database;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.holmeslei.greendaodemo.entity.Employee;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "EMPLOYEE".
*/
public class EmployeeDao extends AbstractDao<Employee, Long> {

    public static final String TABLENAME = "EMPLOYEE";

    /**
     * Properties of entity Employee.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property CompanyId = new Property(1, long.class, "companyId", false, "COMPANY_ID");
        public final static Property EmployeeName = new Property(2, String.class, "employeeName", false, "EMPLOYEE_NAME");
        public final static Property Salary = new Property(3, int.class, "salary", false, "SALARY");
    }

    private Query<Employee> company_EmployeeListQuery;

    public EmployeeDao(DaoConfig config) {
        super(config);
    }
    
    public EmployeeDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"EMPLOYEE\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"COMPANY_ID\" INTEGER NOT NULL ," + // 1: companyId
                "\"EMPLOYEE_NAME\" TEXT," + // 2: employeeName
                "\"SALARY\" INTEGER NOT NULL );"); // 3: salary
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"EMPLOYEE\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Employee entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getCompanyId());
 
        String employeeName = entity.getEmployeeName();
        if (employeeName != null) {
            stmt.bindString(3, employeeName);
        }
        stmt.bindLong(4, entity.getSalary());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Employee entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getCompanyId());
 
        String employeeName = entity.getEmployeeName();
        if (employeeName != null) {
            stmt.bindString(3, employeeName);
        }
        stmt.bindLong(4, entity.getSalary());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Employee readEntity(Cursor cursor, int offset) {
        Employee entity = new Employee( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getLong(offset + 1), // companyId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // employeeName
            cursor.getInt(offset + 3) // salary
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Employee entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setCompanyId(cursor.getLong(offset + 1));
        entity.setEmployeeName(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setSalary(cursor.getInt(offset + 3));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Employee entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Employee entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Employee entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "employeeList" to-many relationship of Company. */
    public List<Employee> _queryCompany_EmployeeList(long companyId) {
        synchronized (this) {
            if (company_EmployeeListQuery == null) {
                QueryBuilder<Employee> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CompanyId.eq(null));
                company_EmployeeListQuery = queryBuilder.build();
            }
        }
        Query<Employee> query = company_EmployeeListQuery.forCurrentThread();
        query.setParameter(0, companyId);
        return query.list();
    }

}
